// 用于创建vuex核心store

// 引入vue
import Vue from "vue";
import Vuex from 'vuex';
Vue.use(Vuex)

// 准备actions - 响应组件动作
const actions = {
    jia(context, value) {
        //console.log('Action Jia', context, value);
        context.commit('JIA', value);
    },
    jian(context, value) {
        context.commit('JIAN', value);
    },
    jiaOdd(context, value) {
        console.log('Action jiaOdd处理了一些事情');
        context.dispatch('demo1', value);
    },
    jiaWait(context, value) {
        setTimeout(() => {
            context.commit('JIA', value);
        }, 500);
    },
    demo1(context, value) {
        console.log('demo1处理了一些事情');
        if (0 !== context.state.sum % 2) {
            context.commit('JIA', value);
        }
    },
};
// 用于操作数据
const mutations = {
    JIA(state, value) {
        //console.log('mutations.jia',state,value);
        state.sum += value;
    },
    JIAN(state, value) {
        state.sum -= value;
    },
    ADD_PERSON(state, value){
        state.personList.unshift(value);
    },
};
// 用于存储数据
const state = {
    sum: 0,
    school: '培立优',
    subject: 'JAVA',
    personList:[
        {id:'001',name:'张三'},
    ]
};

const getters = {
    bigSum(state){
        return state.sum * 10;
    }
};

//创建并导出store
export default new Vuex.Store({
    actions: actions,
    mutations,//重名 简写
    state,
    getters,
});